package my.jwds.p112;

import my.jwds.p94.TreeNode;

import java.util.HashSet;

public class HasPathSum {
    public static void main(String[] args) {
        HasPathSum o = new HasPathSum();
        TreeNode node = TreeNode.to_binaryHaveNull(new Integer[]{5,4,8});
        System.out.println(o.hasPathSum(node,22));
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        return hasPathSum(root,targetSum,0);
    }
    public boolean hasPathSum(TreeNode root, int targetSum,int sum) {
        if (root==null){
            return false;
        }
        sum += root.val;
        if (root.left == null && root.right == null && sum == targetSum){
            return true;
        }
        return hasPathSum(root.left, targetSum, sum) || hasPathSum(root.right, targetSum, sum);
    }
}
